

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" type="image/png" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="CodeHope">
  <meta name="author" content="CodeHope">
  <meta name="keywords" content="希望">
  <title>SpringBoot框架的入门 - CodeHope</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      
        
          
          
          
        
        <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/prismjs@1.22.0/themes/prism-tomorrow.min.css" />
      
      
        <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/prismjs@1.22.0/plugins/line-numbers/prism-line-numbers.min.css" />
      
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"quancundexiwang.wang","root":"/","version":"1.8.7","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":true,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"onlypost":true},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"Mi65hxq7VAFUDwOLeIGAOgiV-gzGzoHsz","app_key":"hMuhiD4FRqhns4giqLiEH9HG","server_url":null}}};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.3.0"></head>


<body>
  <header style="height: 80vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>CodeHope</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                村头
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                我
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('http://qiniuyun.quancundexiwang.wang/20210502162555.png') repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="SpringBoot框架的入门">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-05-02 16:25" pubdate>
        2021年5月2日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      3k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      38
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
            <div class="scroll-down-bar">
              <i class="iconfont icon-arrowdown"></i>
            </div>
          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">SpringBoot框架的入门</h1>
            
            <div class="markdown-body">
              <h1 id="SpringBoot框架的入门"><a href="#SpringBoot框架的入门" class="headerlink" title="SpringBoot框架的入门"></a>SpringBoot框架的入门</h1><h2 id="1-为什么用SpringBoot"><a href="#1-为什么用SpringBoot" class="headerlink" title="1.为什么用SpringBoot"></a>1.为什么用SpringBoot</h2><blockquote>
<p>Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.</p>
<p>能快速创建出生产级别的Spring应用</p>
</blockquote>
<p>其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置，从而使开发人员不再需要定义样板化的配置它默认配置了很多框架的使用方式，就像 Maven 整合了所有的 Jar 包，Spring Boot 整合了所有的框架。</p>
<p><strong>Spring Boot四大核心</strong></p>
<ul>
<li><code>EnableAutoConfiguration</code> 自动装配</li>
<li><code>Starter</code>组件, 开箱即用</li>
<li><code>Actuator</code> 监控</li>
<li><code>Spring Boot Cli</code> 为<code>Spring Cloud</code>提供了<code>Spring Boot</code> 命令行功能</li>
</ul>
<h2 id="2-SpringBoot优点"><a href="#2-SpringBoot优点" class="headerlink" title="2.SpringBoot优点"></a>2.SpringBoot优点</h2><ol>
<li>创建独立Spring应用</li>
<li>内嵌web服务器</li>
<li>自动starter依赖，简化构建配置</li>
<li>自动配置Spring以及第三方功能</li>
<li>提供生产级别的监控、健康检查及外部化配置</li>
<li>无代码生成、无需编写XML</li>
</ol>
<h2 id="3-SpringBoot缺点"><a href="#3-SpringBoot缺点" class="headerlink" title="3.SpringBoot缺点"></a>3.SpringBoot缺点</h2><ul>
<li>人称版本帝，迭代快，需要时刻关注变化</li>
<li>封装太深，内部原理复杂，不容易精通</li>
</ul>
<h2 id="4-GET-START"><a href="#4-GET-START" class="headerlink" title="4.GET START"></a>4.GET START</h2><h3 id="a-创建maven工程-引入依赖"><a href="#a-创建maven工程-引入依赖" class="headerlink" title="a.) 创建maven工程,引入依赖"></a>a.) 创建maven工程,引入依赖</h3><div class="code-wrapper"><pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8"?></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>project</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0<span class="token punctuation">"</span></span>
         <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span>
         <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>modelVersion</span><span class="token punctuation">></span></span>4.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>modelVersion</span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.example<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>code.hope.springboot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.0-SNAPSHOT<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>parent</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-parent<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.3.4.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>parent</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-web<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>properties</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>maven.compiler.source</span><span class="token punctuation">></span></span>15<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>maven.compiler.source</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>maven.compiler.target</span><span class="token punctuation">></span></span>15<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>maven.compiler.target</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>properties</span><span class="token punctuation">></span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>project</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<h3 id="b-创建主程序"><a href="#b-创建主程序" class="headerlink" title="b) 创建主程序"></a>b) 创建主程序</h3><p><code>MainApplication.java</code> 直接放置在你的项目根目录下面</p>
<div class="code-wrapper"><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>codeHope</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span></span><span class="token class-name">SpringApplication</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>autoconfigure<span class="token punctuation">.</span></span><span class="token class-name">SpringBootApplication</span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@SpringBootApplication</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MainApplication</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">MainApplication</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<h3 id="c-编写第一个控制器"><a href="#c-编写第一个控制器" class="headerlink" title="c) 编写第一个控制器"></a>c) 编写第一个控制器</h3><p><code>HelloSpringBoot.java</code></p>
<div class="code-wrapper"><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>codeHope<span class="token punctuation">.</span>controller</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>stereotype<span class="token punctuation">.</span></span><span class="token class-name">Controller</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">RequestMapping</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">ResponseBody</span><span class="token punctuation">;</span>

<span class="token comment">//@Controller 表示这个是一个控制器</span>
<span class="token annotation punctuation">@Controller</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HelloSpringBoot</span> <span class="token punctuation">&#123;</span>

    <span class="token comment">// @ResponseBody 表示返回的是return的内容，而不是页面的跳转</span>
    <span class="token annotation punctuation">@ResponseBody</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"/hello"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">helloSpringBoot</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"模拟调用service层"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">"hello springboot"</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p style='color:#035766;background:#feb856;padding:10px;font-weight:bolder;border-radius:15px;line-height:30px;'>@Controller 表示这个是一个控制器<br/>@ResponseBody 表示返回的是return的内容，而不是之前学习springMVC那时候页面的跳转</p>

<p>如果这个控制器的所有方法都是返回return的内容，那么可以使用@RestController注解代替</p>
<div class="code-wrapper"><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>codeHope<span class="token punctuation">.</span>controller</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>stereotype<span class="token punctuation">.</span></span><span class="token class-name">Controller</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">RequestMapping</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">ResponseBody</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">RestController</span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@RestController</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HelloSpringBoot</span> <span class="token punctuation">&#123;</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"/hello"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">helloSpringBoot</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"模拟调用service层"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">"hello springboot"</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>


<p>那么我们现在启动我们的springboot项目！</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210422101255160.png" srcset="/img/loading.gif" alt="image-20210422101255160"></p>
<p>可以看到我们的springboot服务已经成功启动了，默认在本地的8080端口，我们访问测试一下 <a target="_blank" rel="noopener" href="http://localhost:8080/hello">http://localhost:8080/hello</a></p>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210422101435927.png" srcset="/img/loading.gif" alt="image-20210422101435927"></p>
<p>可以成功的访问到我们的控制器。</p>
<h2 id="5-SpringBoot的配置文件"><a href="#5-SpringBoot的配置文件" class="headerlink" title="5.SpringBoot的配置文件"></a>5.SpringBoot的配置文件</h2> <p style='color:#eaf1f8;background:#443da6;padding:10px;font-weight:bolder;border-radius:15px;line-height:30px;'>【SpringBoot有默认的一份配置，但是也支持我们去修改一些配置！我们可以通过创建两种配置文件来修改springboot服务中的一些配置项】</p>

<ol>
<li><code>application.properties</code></li>
<li><code>application.yaml</code></li>
</ol>
<p>配置文件通常还是放在resources目录下面，我们编写配置文件来修改服务的端口试一下：</p>
<p><code>resources&gt;application.yml</code></p>
<div class="code-wrapper"><pre class="line-numbers language-yml" data-language="yml"><code class="language-yml">server:
  port: 8888<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></div>
<p>重启springboot服务</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210422102218270.png" srcset="/img/loading.gif" alt="image-20210422102218270"></p>
<p>可以看到我们已经成功的修改了springboot的默认配置的服务端口！那么启动了我们的第一个spring boot项目后，我们结合我们之前的spring知识，来研究一下我们的springboot。</p>
<h2 id=""><a href="#" class="headerlink" title=""></a></h2><h2 id="6-Conditional条件注解"><a href="#6-Conditional条件注解" class="headerlink" title="6.@Conditional条件注解"></a>6.<code>@Conditional</code>条件注解</h2><h3 id="6-1-概要"><a href="#6-1-概要" class="headerlink" title="6.1 概要"></a>6.1 概要</h3><p style='color:#815abd;background:#fcdc01;padding:10px;font-weight:bolder;border-radius:15px;line-height:30px;'>【@Conditional根据满足某一个特定条件创建一个特定的Bean。比方说，当某一个jar包在一个类路径下的时候，自动配置一个或多个Bean；或者只有某个Bean被创建才会创建另外一个Bean。总的来说，就是根据特定条件来控制Bean的创建行为，这样我们可以利用这个特性进行一些自动的配置。】</p>

<h3 id="6-2-自定义条件注解"><a href="#6-2-自定义条件注解" class="headerlink" title="6.2 自定义条件注解"></a>6.2 自定义条件注解</h3><h4 id="a-开发步骤"><a href="#a-开发步骤" class="headerlink" title="a) 开发步骤"></a>a) 开发步骤</h4><ol>
<li>写一个类继承 <code>Condition</code>接口（<code>import org.springframework.context.annotation.Condition;</code>）并重写其<code>matches</code>方法，返回值为布尔类型<code>boolean</code>，代表是否满足条件！</li>
<li>开发业务代码，业务类。</li>
<li>使用配置类（<code>@Configuration</code>）配置bean(<code>@Bean</code>)或者使用<code>@Component</code>注入组件的时候，使用<code>@Conditional(xxxxCondition.class)</code>注解，并传入自定义的条件类！根据条件是否满足来决定是否向spring工厂中注入这个bean.</li>
</ol>
<h4 id="b-案例实战"><a href="#b-案例实战" class="headerlink" title="b) 案例实战"></a>b) 案例实战</h4><blockquote>
<p>项目运行在不同的系统环境，访问控制器，返回不同系统下的操作命令</p>
</blockquote>
<p>我们就返回在Mac，Linux，Windows中展示当前目录文件或文件夹的命令</p>
<ol>
<li><p>开发<code>condition</code>,根据当前的操作系统环境，返回是否满足某一种操作系统，那么肯定只有一个是true</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428140036290.png" srcset="/img/loading.gif" alt="image-20210428140036290"></p>
</li>
<li><p>开发业务</p>
<p> 基于接口，那么首先定义一个操作系统服务的接口，<code>OsService.java</code>,里面有我们需要的 <code>showListCmd</code>业务方法</p>
 <div class="code-wrapper"><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>codehope<span class="token punctuation">.</span>ch<span class="token punctuation">.</span>service</span><span class="token punctuation">;</span>
  
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">OsService</span> <span class="token punctuation">&#123;</span>
    <span class="token class-name">String</span> <span class="token function">showListCmd</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p> 然后不同的操作系统下有着不同的服务的实现</p>
<p> <img src="http://qiniuyun.quancundexiwang.wang/image-20210428140750030.png" srcset="/img/loading.gif" alt="image-20210428140750030"></p>
</li>
<li><p>使用配置类（<code>@Configuration</code>）配置bean(<code>@Bean</code>)，并且使用<code>@Conditional(xxxxCondition.class)</code>注解，并传入自定义的条件类！根据条件是否满足来决定是否向spring工厂中注入这个<code>Bean</code>.</p>
<p> <code>MyConfig.java</code></p>
 <div class="code-wrapper"><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>codehope<span class="token punctuation">.</span>ch<span class="token punctuation">.</span>config</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>codehope<span class="token punctuation">.</span>ch<span class="token punctuation">.</span>condition<span class="token punctuation">.</span></span><span class="token class-name">LinuxOsCondition</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>codehope<span class="token punctuation">.</span>ch<span class="token punctuation">.</span>condition<span class="token punctuation">.</span></span><span class="token class-name">MacOsCondition</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>codehope<span class="token punctuation">.</span>ch<span class="token punctuation">.</span>condition<span class="token punctuation">.</span></span><span class="token class-name">WindowsOsCondition</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>codehope<span class="token punctuation">.</span>ch<span class="token punctuation">.</span>serviceImpl<span class="token punctuation">.</span></span><span class="token class-name">LinuxOsService</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>codehope<span class="token punctuation">.</span>ch<span class="token punctuation">.</span>serviceImpl<span class="token punctuation">.</span></span><span class="token class-name">MacOsService</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>codehope<span class="token punctuation">.</span>ch<span class="token punctuation">.</span>serviceImpl<span class="token punctuation">.</span></span><span class="token class-name">WindowsOsService</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>context<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Bean</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>context<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Conditional</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>context<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Configuration</span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@Configuration</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyConfig</span> <span class="token punctuation">&#123;</span>

    <span class="token annotation punctuation">@Bean</span>
    <span class="token annotation punctuation">@Conditional</span><span class="token punctuation">(</span><span class="token class-name">WindowsOsCondition</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">WindowsOsService</span> <span class="token function">windowsOsService</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">WindowsOsService</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token annotation punctuation">@Bean</span>
    <span class="token annotation punctuation">@Conditional</span><span class="token punctuation">(</span><span class="token class-name">LinuxOsCondition</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">LinuxOsService</span> <span class="token function">linuxOsService</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">LinuxOsService</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>

    <span class="token annotation punctuation">@Bean</span>
    <span class="token annotation punctuation">@Conditional</span><span class="token punctuation">(</span><span class="token class-name">MacOsCondition</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">MacOsService</span> <span class="token function">macOsService</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">MacOsService</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p> 虽然这个接口有三个实现类！但是我们业务中，放入spring容器的其实只能有一个！是根据<code>@Conditional</code>注解去判断的！</p>
<p> 或者<code>@Component</code>，添加到容器中的时候也可以加入条件注解</p>
<p> <img src="http://qiniuyun.quancundexiwang.wang/image-20210428142752746.png" srcset="/img/loading.gif" alt="image-20210428142752746"></p>
</li>
<li><p>控制器开发！我们访问控制器，返回给view层，对应的结果！</p>
<p> <img src="http://qiniuyun.quancundexiwang.wang/image-20210428141151607.png" srcset="/img/loading.gif" alt="image-20210428141151607"></p>
</li>
<li><p>访问测试 <a target="_blank" rel="noopener" href="http://localhost:8080/queryListCmd">http://localhost:8080/queryListCmd</a></p>
<p> <img src="http://qiniuyun.quancundexiwang.wang/image-20210428141235293.png" srcset="/img/loading.gif" alt="image-20210428141235293"></p>
<p> 因为我们是在Windows环境下，所以<code>@Conditional</code> 就只有Windows的满足，所以spring工厂讲Windows操作系统的服务放入了spring容器，然后我们通过@Autowired注入的也就是 <code>WindowsOsService</code>.</p>
</li>
</ol>
<h3 id="6-3-Spring提供的很多成品注解"><a href="#6-3-Spring提供的很多成品注解" class="headerlink" title="6.3 Spring提供的很多成品注解"></a>6.3 Spring提供的很多成品注解</h3><ol>
<li><p><code>Class Conditions</code></p>
<ul>
<li><p><code>@ConditionalOnClass</code> 当指定的Class存在时，才会注入</p>
</li>
<li><p> <code>@ConditionalOnMissingClass</code> 当指定的Class不存在时，才会注入</p>
</li>
</ul>
</li>
<li><p><code>Bean Conditions</code></p>
<ul>
<li><p><code>@ConditionalOnBean</code> 当指定的Bean存在时，才会注入</p>
</li>
<li><p><code>@ConditionalOnMissingBean</code> 当指定的Bean不存在时，才会注入</p>
</li>
</ul>
</li>
<li><p><code>Property Conditions</code></p>
<ul>
<li><p><code>@ConditionalOnProperty</code></p>
<p>  <code>prefix: name</code>的前缀，可以指定多个<code>name</code>(数组表示），<code>prefix</code>作用于每一个元素</p>
<p>  <code>name：application.properties</code>中的属性名（若存在）</p>
<p>  <code>havingValue</code>：属性的期望值，若与<code>application.properties</code>的对应值相等，则匹配</p>
<p>  <code>matchIfMissing</code>：如果<code>application.properties</code>不存在指定属性，可以指定是否匹配；默认值为<code>false</code>，即不匹配</p>
</li>
</ul>
</li>
</ol>
<p>更多的条件注解的使用！还是需要更多业务需要配合着学习使用的~</p>
<h2 id="7-SpringBoot剖析"><a href="#7-SpringBoot剖析" class="headerlink" title="7. SpringBoot剖析"></a>7. SpringBoot剖析</h2><h3 id="7-1-SpringBoot中的容器"><a href="#7-1-SpringBoot中的容器" class="headerlink" title="7.1 SpringBoot中的容器"></a>7.1 SpringBoot中的容器</h3><p>其实SpringBoot项目启动的时候是有返回值的！那就是Spring的工厂【IOC容器】管理着spring工厂中的组件</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428103700400.png" srcset="/img/loading.gif" alt="image-20210428103700400"></p>
<div class="code-wrapper"><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@SpringBootApplication</span>
<span class="token annotation punctuation">@ComponentScan</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"com"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ChApplication</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">ConfigurableApplicationContext</span> context <span class="token operator">=</span> <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">ChApplication</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">String</span> beanDefinitionName <span class="token operator">:</span> context<span class="token punctuation">.</span><span class="token function">getBeanDefinitionNames</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>beanDefinitionName<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428110857524.png" srcset="/img/loading.gif" alt="image-20210428110857524"></p>
<h3 id="7-2-SpringBoot的组件扫描"><a href="#7-2-SpringBoot的组件扫描" class="headerlink" title="7.2 SpringBoot的组件扫描"></a>7.2 SpringBoot的组件扫描</h3> <p style='color:#eaf1f8;background:#443da6;padding:10px;font-weight:bolder;border-radius:15px;line-height:30px;'>【SpringBoot的主程序！其实也是一个配置类，并且开启了`@ComponentScan`组件的注解扫描！所以我们的组件注解都会被放入容器，交由Spring管理！】</p>

<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428104352196.png" srcset="/img/loading.gif" alt="image-20210428104352196"></p>
<p>​    默认扫描当前主程序所在的包，及其子包下面的注解！</p>
<p>​    <img src="http://qiniuyun.quancundexiwang.wang/image-20210428110024129.png" srcset="/img/loading.gif" alt="image-20210428110024129"></p>
<p>访问 <a target="_blank" rel="noopener" href="http://localhost:8080/out">http://localhost:8080/out</a></p>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428110132812.png" srcset="/img/loading.gif" alt="image-20210428110132812"></p>
<p>但是我们可以手动修改这个包扫描器的范围【注意不能和默认范围一样，如果修改的扫描范围等于默认的范围，就会报错！】</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428110349655.png" srcset="/img/loading.gif" alt="image-20210428110349655"></p>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428110702821.png" srcset="/img/loading.gif" alt="image-20210428110702821"></p>
<p>当我们指定范围为<code>com</code> 之后，访问<a target="_blank" rel="noopener" href="http://localhost:8080/out">http://localhost:8080/out</a> 发现就生效了</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428110726274.png" srcset="/img/loading.gif" alt="image-20210428110726274"></p>
<h3 id="7-3-SpringBoot的自动装配"><a href="#7-3-SpringBoot的自动装配" class="headerlink" title="7.3 SpringBoot的自动装配"></a>7.3 SpringBoot的自动装配</h3><ul>
<li><p>为什么要有自动的装配？</p>
<p style='color:#815abd;background:#fcdc01;padding:10px;font-weight:bolder;border-radius:15px;line-height:30px;'>【我们之前开发spring应用的时候，需要配置很多的第三方包，ORM框架，Tomcat,Redis,数据库驱动,,,还要写大量的配置文件，而且你还要考虑这么多第三方包之间的版本关系，版本错了就可能整个项目完全运行不起来！ 出现各种各样的问题】</p>

<img src="http://qiniuyun.quancundexiwang.wang/image-20210428174313300.png" srcset="/img/loading.gif" style="zoom:50%;" />

<p>那么相对于这个，SpringBoot的开箱即用就显得特别方便,他的<code>spring-boot-dependencies</code>(父项目)存放了SpringBoot的核心依赖。我们在写或者引入一些SpringBoot依赖的时候，不需要指定版本，正是因为SpringBoot的父依赖已经帮我们维护了一套版本。</p>
</li>
<li><p>自动装配的原理</p>
<blockquote>
<p>每一个包都有自己的一份配置bean,并且按照规则写一些配置，SpringBoot会将所有的第三方配置整入到自己的配置类中！这样spring容器，就接管了所有第三方包的类</p>
</blockquote>
<p>更详细的参考下面的 【SpringBoot自动装配的原理】</p>
</li>
</ul>
<h2 id="8-SpringBoot自动装配的原理"><a href="#8-SpringBoot自动装配的原理" class="headerlink" title="8.SpringBoot自动装配的原理"></a>8.SpringBoot自动装配的原理</h2><p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428185343965.png" srcset="/img/loading.gif" alt="image-20210428185343965"></p>
<h3 id="1-剖析SpringBoot的启动类"><a href="#1-剖析SpringBoot的启动类" class="headerlink" title="1. 剖析SpringBoot的启动类"></a>1. 剖析SpringBoot的启动类</h3><p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428184340568.png" srcset="/img/loading.gif" alt="image-20210428184340568"></p>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428184359772.png" srcset="/img/loading.gif" alt="image-20210428184359772"></p>
<p>其中：</p>
<p><code>@ComponentScan</code>,不需要多说，只是为了扫描当前类下的包，及其子包的组件注解</p>
<p><code>@SpringBootConfiguration</code> 表明这个类是一个主的配置类！这个配置类是能引入第三方包的配置类的核心！</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428184519688.png" srcset="/img/loading.gif" alt="image-20210428184519688"></p>
<p>重点我们下面就要好好的看一看这个<code>@EnableAutoConfiguration</code></p>
<h3 id="2-探究-EnableAutoConfiguration"><a href="#2-探究-EnableAutoConfiguration" class="headerlink" title="2. 探究 @EnableAutoConfiguration"></a>2. 探究 <code>@EnableAutoConfiguration</code></h3><p>我们发现这里</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428220959288.png" srcset="/img/loading.gif" alt="image-20210428220959288"></p>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428220927862.png" srcset="/img/loading.gif" alt="image-20210428220927862"></p>
<p>这个文件其实到时候，你的第三方包的配置类的全路径都会存在这里！</p>
<p>然后会被这个类读取</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428214744532.png" srcset="/img/loading.gif" alt="image-20210428214744532"></p>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428220246570.png" srcset="/img/loading.gif" alt="image-20210428220246570"></p>
<p>上面的步骤用文字来描述就是，这个<code>@EnableAutoConfiguration</code>注解，配置 <code>AutoConfigurationImportSelector.class</code>,这个AutoConfigurationImportSelector会进行一些类操作去读取<code>Spring.factories</code>这个文件，而这个文件是什么呢？</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428220927862.png" srcset="/img/loading.gif" alt="image-20210428220927862"></p>
<p>如图存了一些第三方包配置类的全路径名！那么他就会将这些配置类，全部搞到一个字符串数组中去，这样spring工厂的主配置类，就可以引入第三方的配置文件，那么也就可以管理第三方的bean了！</p>
<h3 id="3-自己写一个springboot-starter"><a href="#3-自己写一个springboot-starter" class="headerlink" title="3. 自己写一个springboot-starter"></a>3. 自己写一个springboot-starter</h3><p>首先模拟一个<code>ImportSelector</code>用于收集第三方包的配置类，当然我这里把收集到的配置类的路径都写死了！</p>
<div class="code-wrapper"><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>tt<span class="token punctuation">.</span></span><span class="token class-name">ImprotSelector</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>context<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">DeferredImportSelector</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>core<span class="token punctuation">.</span>type<span class="token punctuation">.</span></span><span class="token class-name">AnnotationMetadata</span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ImportSelector</span> <span class="token keyword">implements</span> <span class="token class-name">DeferredImportSelector</span> <span class="token punctuation">&#123;</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">selectImports</span><span class="token punctuation">(</span><span class="token class-name">AnnotationMetadata</span> annotationMetadata<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">&#123;</span><span class="token class-name">MyOwnPackageConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>MyOwnPackageConfig.java</code> 这个配置类，配置了一些属于我自己的包的类</p>
<div class="code-wrapper"><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>tt<span class="token punctuation">.</span></span><span class="token class-name">ImprotSelector</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>context<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Bean</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>context<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Configuration</span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@Configuration</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyOwnPackageConfig</span> <span class="token punctuation">&#123;</span>
    <span class="token annotation punctuation">@Bean</span>
    <span class="token keyword">public</span> <span class="token class-name">User</span> <span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>然后写我们的springboot的启动类【我们这里把web服务的启动关闭了<code> .web(WebApplicationType.NONE)</code>】</p>
<div class="code-wrapper"><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>codehope<span class="token punctuation">.</span>ch</span><span class="token punctuation">;</span>


<span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>tt<span class="token punctuation">.</span></span><span class="token class-name">ImprotSelector<span class="token punctuation">.</span>ImportSelector</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span></span><span class="token class-name">SpringApplication</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span></span><span class="token class-name">WebApplicationType</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>builder<span class="token punctuation">.</span></span><span class="token class-name">SpringApplicationBuilder</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>context<span class="token punctuation">.</span></span><span class="token class-name">ConfigurableApplicationContext</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>context<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">ComponentScan</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>context<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Import</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>stereotype<span class="token punctuation">.</span></span><span class="token class-name">Component</span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@ComponentScan</span><span class="token punctuation">(</span><span class="token string">"com.tt"</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@Import</span><span class="token punctuation">(</span><span class="token class-name">ImportSelector</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyOwnStarter</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token comment">//这个其实就是spring的工厂</span>
        <span class="token class-name">ConfigurableApplicationContext</span> context <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SpringApplicationBuilder</span><span class="token punctuation">(</span><span class="token class-name">MyOwnStarter</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
                <span class="token punctuation">.</span><span class="token function">web</span><span class="token punctuation">(</span><span class="token class-name">WebApplicationType</span><span class="token punctuation">.</span>NONE<span class="token punctuation">)</span> <span class="token comment">//关闭启动web服务器</span>
                <span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">String</span> beanDefinitionName <span class="token operator">:</span> context<span class="token punctuation">.</span><span class="token function">getBeanDefinitionNames</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>beanDefinitionName<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428223636817.png" srcset="/img/loading.gif" alt="image-20210428223636817"></p>
<p>运行，我们发现，我们的spring工厂也能收集配置类来实现bean的管理</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/image-20210428223743500.png" srcset="/img/loading.gif" alt="image-20210428223743500"></p>
<h3 id="4-总结一下"><a href="#4-总结一下" class="headerlink" title="4.总结一下"></a>4.总结一下</h3><blockquote>
<p>springboot的自动装配,能完成自动装配，也离开IOC容器，因为有了IOC容器，才能让第三方包配置bean都自动的收集，并且交给spring去管理，那么有这些功能，那么当然就能完成springboot的自动配置，开箱即用了，也不用我们开发者自己去维护很多我们需要的第三方包的配置。</p>
</blockquote>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/Java/">Java</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/SpringBoot%E5%9F%BA%E7%A1%80/">SpringBoot基础</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/posts/53229/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">SpringBoot全局异常处理</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/posts/28611/">
                        <span class="hidden-mobile">Spring纯注解版AOP</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments">
                
                
  <div id="vcomments"></div>
  <script type="text/javascript">
    Fluid.utils.waitElementVisible('vcomments', function() {
      Fluid.utils.createScript('https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js', function () {
        new Valine({
          el: "#vcomments",
          app_id: "Mi65hxq7VAFUDwOLeIGAOgiV-gzGzoHsz",
          app_key: "hMuhiD4FRqhns4giqLiEH9HG",
          placeholder: "说点什么",
          path: window.location.pathname,
          avatar: "retro",
          meta: ["nick","mail","link"],
          pageSize: "10",
          lang: "zh-CN",
          highlight: false,
          recordIP: false,
          serverURLs: "",
        });
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the
    <a target="_blank" href="https://valine.js.org" rel="nofollow noopener noopener">comments powered by Valine.</a>
  </noscript>


              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
    

  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        豫ICP备20009912号-1
      </a>
    </span>
    
  </div>


  
</footer>

<!-- SCRIPTS -->

  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":true,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    
      <script  src="/js/lazyload.js" ></script>
    
  



  
    
  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.0/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.0/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    (function () {
      var path = "http://qiniuyun.quancundexiwang.wang/xmlSearch.xml";
      var inputArea = document.querySelector("#local-search-input");
      inputArea.onclick = function () {
        searchFunc(path, 'local-search-input', 'local-search-result');
        this.onclick = null
      }
    })()
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>



</body>
</html>
